package com.example.screenapppush;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.IBinder;
import android.os.Parcelable;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

public class ScreenPushService extends Service {
    private String NOTIFICATION_CHANNEL_ID = "ScreenPushService_nofity";
    private String NOTIFICATION_CHANNEL_NAME = "ScreenPushService";
    private String NOTIFICATION_CHANNEL_DESC = "screen recoder desc";
    private int NOTIFICATION_ID = 1000;
    private static final String NOTIFICATION_TICKER = "ScreenPushApp";
    private String TAG="ScreenPushService";
    SocketLive socketLive;
    @Override
    public void onCreate() {
        super.onCreate();
        createNotification();
         socketLive = new SocketLive();
         socketLive.startWebSocketServer();
    }

    public void createNotification() {
        Log.i("ScreenPushService", "notification: " + Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //Call Start foreground with notification
            Intent notificationIntent = new Intent(this, ScreenPushService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground))
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("Starting Service")
                    .setContentText("Starting monitoring service")
                    .setTicker(NOTIFICATION_TICKER)
                    .setContentIntent(pendingIntent);
            Notification notification = notificationBuilder.build();
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(NOTIFICATION_CHANNEL_DESC);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
            startForeground(NOTIFICATION_ID, notification);
            //notificationManager.notify(NOTIFICATION_ID, notification);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        int resultCode = intent.getIntExtra("resultCode", -1);
        Intent resultData = intent.getParcelableExtra("data");
        Log.i(TAG, "onStartCommand: " + resultCode);
        Log.i(TAG, "onStartCommand: " + resultData);
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, resultData);
        Log.i(TAG, "onStartCommand: " + mediaProjection);
        socketLive.setMediaProjection(mediaProjection);
        socketLive.startLive();


//        AudioPlaybackCaptureConfiguration.Builder builder = new AudioPlaybackCaptureConfiguration.Builder(mediaProjection);
//        builder.addMatchingUsage(AudioAttributes.USAGE_MEDIA);//多媒体
//        builder.addMatchingUsage(AudioAttributes.USAGE_ALARM);//闹铃
//        builder.addMatchingUsage(AudioAttributes.USAGE_GAME);//游戏
//        audioPlaybackCaptureConfiguration = builder.build();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
